package main

import (
	"chat/server/model"
	"fmt"
	"net"
	"time"
)

// 服务启动时初始化全局UserDao

func initUserDao() {
	// 需要先初始化redis pool
	model.MyUserDao = model.NewUserDao(pool)
}

func main() {
	// 服务器启动时初始化redis连接池
	initPool("127.0.0.1:6379", 5000, 16, 300*time.Second)
	initUserDao()

	fmt.Println("服务器在8888端口监听...")
	listen, err := net.Listen("tcp", "0.0.0.0:8888")
	if err != nil {
		fmt.Println("net.Listen error = ", err)
		return
	}
	defer listen.Close()

	for {
		fmt.Println("服务器等待连接")
		conn, err := listen.Accept()
		if err != nil {
			fmt.Println("listen.Accept err = ", err)
			continue
		}
		go process(conn)
	}
}

func process(conn net.Conn) (err error) {
	defer conn.Close()
	// 调用总控
	processor := &Process{
		Conn: conn,
	}
	err = processor.process()
	if err != nil {
		fmt.Println("server main process err = ", err)
		return
	}
	return
}
